﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using QuickGame.View;

namespace QuickGame.Model
{
    public class NguoiDungHs
    {
        public static IQueryable<NguoiDung> QueryEntities(ModelContext modelContext)
        {
            return modelContext.NguoiDungs;
        }

        public static NguoiDung QueryEntity(ModelContext modelContext, long id)
        {
            return modelContext.NguoiDungs.Single(k => k.ID == id);
        }

        public static NguoiDung Insert(ModelContext modelContext)
        {
            NguoiDung nd = new NguoiDung();
            modelContext.NguoiDungs.AddObject(nd);
            return nd;
        }
        public static void Delete(ModelContext modelContext, long id) 
        {
            NguoiDung nd = modelContext.NguoiDungs.Single(k => k.ID == id);
            foreach (var luotchoi in nd.LuotChois.ToList())
	        {
                foreach (var chitiet in luotchoi.ChiTietChois.ToList())
                {
                    modelContext.ChiTietChois.DeleteObject(chitiet);
                }
                modelContext.DeleteObject(luotchoi);
	        }
            modelContext.NguoiDungs.DeleteObject(nd);
        }
        public static void Update(ModelContext modelContext,NguoiDungView view)
        {
            NguoiDung nd = modelContext.NguoiDungs.Single(k => k.ID == view.ID);
            nd.ID = view.ID;
            nd.Ma = view.Ma;
            nd.Ten = view.HoTen;
            nd.NgaySinh = view.NgaySinh;
            nd.GioiTinh = view.GioiTinh;
            nd.Email = view.Email;
            modelContext.SaveChanges();
        }
        //public static IQueryable<KhachHangGridView> QueryKhachHangChoHD(ModelContext modelContext)
        //{
        //    return modelContext.KhachHang.Select(k => new KhachHangGridView
        //    {
        //        Ma = k.Ma,
        //        ID = k.ID,
        //    });
        //}
    }
}